package com.prs.crm.dao.cqunicom.impl;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.cqunicom.OrderDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.cdxh.distribution.DistributionReturn;
import com.prs.crm.domain.cqunicom.Company;
import com.prs.crm.domain.cqunicom.PoplarOrder;
import com.prs.crm.domain.cqunicom.Services;
import com.prs.crm.domain.hr.Person;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-3-2 下午03:17:36
 * 简单说明
 */

public class OrderDaoImpl extends BaseDaoImpl<PoplarOrder> implements OrderDao {

	@Override
	public void saveOrupdate(PoplarOrder order) {
		this.getHibernateTemplate().update(order);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PoplarOrder> getAllOrderList() {
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	criteria.add(Restrictions.eq("newOrderState","1"));				        	
							return criteria.list();
				 				        	
				        }
					}
			);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PoplarOrder> getAllPopOrder(final String date,final Date startTime,final Date endTime,final String dealSate) {
		
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	if ("派单时间".equals(date)) {
								criteria.createAlias("businessOrder", "x");
								criteria.add(Restrictions.between("x.outDate", startTime,endTime));
							} else if ("完成时间".equals(date)) {
								criteria.createAlias("businessOrder", "x");
								criteria.add(Restrictions.between("x.outDate", startTime,endTime));
							} else if ("预约时间".equals(date)) {
								criteria.createAlias("businessOrder", "x");
								criteria.add(Restrictions.between("x.rserveTime",startTime,endTime));
							}
				        	if ("所有".equals(dealSate)) {

							} else {
								criteria.createAlias("businessOrder", "a");
								criteria.add(Restrictions.eq("a.transationSatet", dealSate));
							}
				        	criteria.add(Restrictions.eq("modleName", "派单"));			        	
							return criteria.list();
				 				        	
				        }
					}
			);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PoplarOrder> getServerStylePopOrder(final String date,final Date startTime,final Date endTime,final String dealSate,final Services services) {
		
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	criteria.createAlias("businessOrder", "bu");
				        	if ("派单时间".equals(date)) {
								
								criteria.add(Restrictions.between("bu.outDate", startTime,endTime));
							} else if ("完成时间".equals(date)) {
								
								criteria.add(Restrictions.between("bu.outDate", startTime,endTime));
							} else if ("预约时间".equals(date)) {
								
								criteria.add(Restrictions.between("bu.rserveTime",startTime,endTime));
							}
				        	if ("所有".equals(dealSate)) {

							} else {
								
								criteria.add(Restrictions.eq("bu.transationSatet", dealSate));
							}				        	
				        	criteria.add(Restrictions.eq("services",services));				       	
				        	criteria.add(Restrictions.eq("modleName", "派单"));				        	
							return criteria.list();
				 				        	
				        }
					}
			);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PoplarOrder> getCompanyPopOrder(final String date,final Date startTime,final Date endTime,final String dealSate,final String company) {
		
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	if ("派单时间".equals(date)) {
								criteria.createAlias("businessOrder", "bu");
								criteria.add(Restrictions.between("bu.outDate", startTime,endTime));
							} else if ("完成时间".equals(date)) {
								criteria.createAlias("businessOrder", "bu");
								criteria.add(Restrictions.between("bu.outDate", startTime,endTime));
							} else if ("预约时间".equals(date)) {
								criteria.createAlias("businessOrder", "bu");
								criteria.add(Restrictions.between("bu.rserveTime",startTime,endTime));
							}
				        	if ("所有".equals(dealSate)) {

							} else {
								criteria.createAlias("businessOrder", "bu");
								criteria.add(Restrictions.eq("bu.transationSatet", dealSate));
							}
				        	criteria.add(Restrictions.eq("comName",company));
				        	criteria.add(Restrictions.eq("modleName", "派单"));		        	
							return criteria.list();
				 				        	
				        }
					}
			);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PoplarOrder> getAllBydealState(final String deal) {
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	
				        	if ("所有".equals(deal)) {

							} else {
								criteria.createAlias("businessOrder", "yy");
								criteria.add(Restrictions.eq("yy.transationSatet", deal));
							}
				        	
				        	criteria.add(Restrictions.eq("newOrderState", "2"));	
				        	criteria.add(Restrictions.eq("modleName", "派单"));		        	
							return criteria.list();
				 				        	
				        }
					}
			);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PoplarOrder> getBydealState(final String deal) {
		return (List<PoplarOrder>) this.getHibernateTemplate().execute(
				new HibernateCallback()
				{
					public Object doInHibernate(Session session) throws HibernateException {
			        	Criteria criteria = session.createCriteria(PoplarOrder.class);
			        	
			        	if ("所有".equals(deal)) {

						} else {
						criteria.createAlias("businessOrder", "rr");	
						criteria.add(Restrictions.eq("rr.transationSatet", deal));
						}
			        	
//						criteria.add(Restrictions.isNotEmpty("rr.rserveTime"));
			        	criteria.add(Restrictions.eq("newOrderState", "2"));	
			        	criteria.add(Restrictions.eq("modleName", "派单"));		        	
						return criteria.list();
			 				        	
			        }
				}
		);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PoplarOrder> getByPersonandDate(final String date,final Date startTime,
			final Date endTime,final Person person) {
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	if ("派单时间".equals(date)) {
							
								criteria.add(Restrictions.between("orderDate", startTime,endTime));
							} else if ("完成时间".equals(date)) {
								
								criteria.add(Restrictions.between("compleDate", startTime,endTime));
							}
//				        	criteria.add(Restrictions.eq("modleName", "派单"));	
				        	if(person!=null){
				        	criteria.add(Restrictions.eq("person",person));	
				        	}
							return criteria.list();
				 				        	
				        }
					}
			);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public PoplarOrder getBycusName(final String names) {
		 return (PoplarOrder) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	
				        	criteria.add(Restrictions.eq("comName",names));	
				        	List<PoplarOrder>  gg=criteria.list();
				         if(gg==null){
				        	 return null;
				         }else{
							return gg.get(0);
				         }
				 				        	
				        }
					}
			);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PoplarOrder> getBycompany(final Company com) {
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	
				        	criteria.add(Restrictions.eq("company",com));	
				        
							return criteria.list();
				 				        	
				        }
					}
			);
	}

	@Override
	public List<PoplarOrder> getByPerson(final Person person) {
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	
				        	criteria.add(Restrictions.eq("person",person));	
				        
							return criteria.list();
				 				        	
				        }
					}
			);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PoplarOrder> getDispatchOrder() {
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	
				        	criteria.add(Restrictions.eq("modleName","派单"));	
				        
							return criteria.list();
				 				        	
				        }
					}
			);
	}

	@Override
	public List<PoplarOrder> getTraferOrder() {
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	
				        	criteria.add(Restrictions.eq("modleName","转接"));	
				        
							return criteria.list();
				 				        	
				        }
					}
			);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Integer delOrder() {
		return (Integer) this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
							Query query = session.createQuery("delete from PoplarOrder");
							return query.executeUpdate();
				        }   
				 } );
	}

	@Override
	public List<PoplarOrder> getByServicesOrder(final Services services) {
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	
				        	criteria.add(Restrictions.eq("services",services));	
				        
							return criteria.list();
				 				        	
				        }
					}
			);
	}

	@Override
	public List<PoplarOrder> getByPersons(final Person person) {
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);
				        	
				        	criteria.add(Restrictions.eq("accpectPerson",person));	
				        
							return criteria.list();
				 				        	
				        }
					}
			);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PoplarOrder> getCurrentOrder(final Date startTime,final Date endTime) {
		 return (List<PoplarOrder>) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(PoplarOrder.class);			        	
				        	criteria.add(Restrictions.between("recordTime",startTime,endTime));					        
							return criteria.list();
				 				        	
				        }
					}
			);
	}

	

}
